import { defineStore } from "pinia";
import { ref } from "vue";
import { apiLogin } from "@/api/user.js";
export const useUserStore = defineStore(
  "user",
  () => {
    const token = ref(uni.getStorageSync("token") || "");
    // 用户信息
    const userInfo = ref({});
    //登录存token存userinfo
    const fetchLogin = async (data = {}) => {
      const res = await apiLogin(data);
      userInfo.value = res;
      token.value = res.token;
    };
    return {
      token,
      userInfo,
      fetchLogin,
    };
  },
  {
    persist: {
      key: "user_store",
      pick: ["userInfo", "token"],
      storage: {
        // 自定义存储方式（小程序环境默认会用 uni.setStorageSync）
        setItem: (key, value) => uni.setStorageSync(key, value), // 存储
        getItem: (key) => uni.getStorageSync(key), // 读取
      },
    },
  }
);
